#include "stdafx.h"
#include <MyControls/TimeSlider.h>
#include <MyControls/Timeline.h>
#include <MyControls/Resource.h>
#include <MyControls/App.h>
#include <cmath>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

namespace My {

TimeSlider::TimeSlider(Timeline & t)
	: m_Timeline(t)
{
	m_Timeline.Add(this);
}

String TimeSlider::Hint() const
{
	return GetApp().LoadString(IDS_TIMELINE_TIME_SCALE);
}

bool TimeSlider::Enabled() const
{
	return !m_Timeline.IsEmpty() && m_Timeline.GetMinTimeScale() < m_Timeline.GetMaxTimeScale();
}

double TimeSlider::GetValue() const
{
	if (m_Timeline.IsEmpty())
		return 0;
	else {
		double s = m_Timeline.GetTimeScale(), 
			min = m_Timeline.GetMinTimeScale(), 
			max = m_Timeline.GetMaxTimeScale();
		if (max > min)
			return log(s / min) / log(max / min);
		else 
			return 0;
	}
}

void TimeSlider::SetValue(double v)
{
	if (m_Timeline.IsEmpty())
		return;
	double min = m_Timeline.GetMinTimeScale(), 
		max = m_Timeline.GetMaxTimeScale();
	if (max > min) {
		m_Timeline.SetTimeScale(min * pow(max / min, v));
		m_Timeline.Update();
	}
}

void TimeSlider::ProcessEvent(My::Event & e)
{
	if (e.Is<Sequence::PreviewFrameChange>())
		return;
	if (e.Is<Sequence::Event>() || e.Is<Timeline::Event>()) {
		Invalidate();
		return;
	}
	Slider::ProcessEvent(e);
}

} // My